package com.qf.controller;

import com.qf.pojo.SysUsers;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 提交登录表单
 * @author lixu
 */
@Controller
public class LoginController {

    //登录
    @GetMapping("/doLogin1")
    public String doLogin(String username, String password, boolean rememberMe,Model model){

        Subject subject = SecurityUtils.getSubject();
        Md5Hash md5Hash = new Md5Hash(password,username,1024);
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username,md5Hash.toString());
        //记住我
        usernamePasswordToken.setRememberMe(rememberMe);
        //登录
        try {
            subject.login(usernamePasswordToken);
/*            Session session = subject.getSession();
            SysUsers sysUsers = (SysUsers) subject.getPrincipal();
            session.setAttribute("username",sysUsers.getUsername());*/
            return "redirect:/index.jsp";
        } catch (AuthenticationException e) {
            model.addAttribute("error",e.getMessage());
            return "/login.jsp";
        }
    }
    /**
     * 退出
     *
     */
    @GetMapping("/logout")
    @ResponseBody
    public Map logout(){
        Subject subject = SecurityUtils.getSubject();

        subject.logout();//底层： 清理Session  清理Cookie


        Map map = new HashMap();
        map.put("code",0);
        return map;
    }

    public static void main(String[] args) {

        System.out.println(System.getProperty("java.io.tmpdir"));


    }
}
